class event_handler :
    def __init__ ( self , handler_func , scheduler ) :
        self . _handler_func = handler_func
        self . _scheduler = scheduler
    def _tk_event_handler ( self , event ) :
        self . _scheduler . run ( self . _handler_func )

class gui :

    def __init__ ( self , mediator , tkinter , tkfont , scheduler ) :
        self . _mediator = mediator
        self . _tkinter = tkinter
        self . _tkfont = tkfont
        self . _scheduler = scheduler

    ##################################################################################################

    def gui_create_application_window_request ( self ) :
        class application_window :
            def __init__ ( self , tkinter ) :
                self . _root = tkinter . Tk ( )
            def _get_tk_entity ( self ) :
                return self . _root
        self . _mediator . gui_create_application_window_reply ( application_window ( self . _tkinter ) )
    def gui_set_application_window_icon ( self , window , icon ) :
        window . _get_tk_entity ( ) . wm_iconbitmap ( icon )
    def gui_set_application_window_title ( self , window , title ) :
        window . _get_tk_entity ( ) . wm_title ( title )
    def gui_run_main_application_loop ( self , window ) :
        window . _get_tk_entity ( ) . mainloop ( )
    def gui_quit_application ( self , window ) :
        window . _get_tk_entity ( ) . quit ( )

    ##################################################################################################

    def gui_create_frame_request ( self , master ) :
        class frame :
            def __init__ ( self , tkinter , master ) :
                self . _frame = tkinter . Frame ( master . _get_tk_entity ( ) )
            def _get_tk_entity ( self ) :
                return self . _frame
        self . _mediator . gui_create_frame_reply ( frame ( self . _tkinter , master ) )
    def gui_destroy_frame ( self , frame ) :
        frame . _get_tk_entity ( ) . destroy ( )

    ##################################################################################################

    def gui_create_label_request ( self , master , text , borderwidth , relief , anchor ) :
        class label :
            def __init__ ( self , tkinter , master , text , borderwidth , relief , anchor ) :
                self . _text = tkinter . StringVar ( )
                self . _label = tkinter . Label \
                    ( master . _get_tk_entity ( ) 
                    , text = text 
                    , borderwidth = borderwidth 
                    , relief = relief 
                    , anchor = anchor 
                    , textvariable = self . _text
                    )
                self . _text . set ( text )
            def _get_tk_entity ( self ) :
                return self . _label
            def _get_tk_text_variable ( self ) :
                return self . _text
        self . _mediator . gui_create_label_reply ( label ( self . _tkinter , master , text , borderwidth , relief , anchor ) )
    def gui_set_label_text ( self , label , text ) :
        label . _get_tk_text_variable ( ) . set ( text )

    ##################################################################################################

    def gui_create_image_request ( self , master , data ) :
        class image :
            def __init__ ( self , tkinter , master , data ) :
                self . _image = tkinter . PhotoImage ( data = data )
                self . _label = tkinter . Label ( master . _get_tk_entity ( ) , image = self . _image )
            def _get_tk_entity ( self ) :
                return self . _label
        self . _mediator . gui_create_image_reply ( image ( self . _tkinter , master , data ) )

    ##################################################################################################

    def gui_create_button_request ( self , master , text , command , anchor , borderwidth ) :
        class button :
            def __init__ ( self , tkinter , master , text , command , anchor , borderwidth ) :
                self . _button = tkinter . Button ( master . _get_tk_entity ( ) , text = text , command = command , anchor = anchor , borderwidth = borderwidth )
            def _get_tk_entity ( self ) :
                return self . _button
        self . _mediator . gui_create_button_reply ( button ( self . _tkinter , master , text , command , anchor , borderwidth ) )

    ##################################################################################################

    def gui_create_entry_request ( self , master , borderwidth ) :
        class entry :
            def __init__ ( self , tkinter , master , borderwidth ) :
                self . _text = tkinter . StringVar ( )
                self . _entry = tkinter . Entry ( master . _get_tk_entity ( ) , borderwidth = borderwidth , textvariable = self . _text )
            def _get_tk_entity ( self ) :
                return self . _entry
            def _get_tk_text_variable ( self ) :
                return self . _text
        self . _mediator . gui_create_entry_reply ( entry ( self . _tkinter , master , borderwidth ) )
    def gui_set_entry_text ( self , entry , text ) :
        entry . _get_tk_text_variable ( ) . set ( text )
        entry . _get_tk_entity ( ) . icursor ( self . _tkinter . END )
    def gui_entry_text_request ( self , entry ) :
        self . _mediator . gui_entry_text_reply ( entry . _get_tk_text_variable ( ) . get ( ) )
    def gui_destroy_entry ( self , entry ) :
        entry . _get_tk_entity ( ) . destroy ( )

    ##################################################################################################

    def gui_create_text_request ( self , master ) :
        class text :
            def __init__ ( self , tkinter , master ) :
                self . _text = tkinter . Text ( master . _get_tk_entity ( ) )
            def _get_tk_entity ( self ) :
                return self . _text
        self . _mediator . gui_create_text_reply ( text ( self . _tkinter , master ) )
    def gui_enable_text_edit ( self , text_widget ) :
        text_widget . _get_tk_entity ( ) . configure ( state = self . _tkinter . NORMAL )
    def gui_disable_text_edit ( self , text_widget ) :
        text_widget . _get_tk_entity ( ) . configure ( state = self . _tkinter . DISABLED )
    def gui_add_string_to_text_widget ( self , text_widget , string ) :
        text_widget . _get_tk_entity ( ) . insert ( self . _tkinter . END , string )
    def gui_add_mark_to_text_widget ( self , text_widget , mark_name ) :
        text_widget . _get_tk_entity ( ) . mark_set ( mark_name , self . _tkinter . INSERT )
        text_widget . _get_tk_entity ( ) . mark_gravity ( mark_name , self . _tkinter . LEFT )
    def gui_add_widget_to_text_widget ( self , text_widget , widget ) :
        text_widget . _get_tk_entity ( ) . window_create ( self . _tkinter . END , window = widget . _get_tk_entity ( ) )
    def gui_remove_widget_from_text_widget ( self , text_widget , widget ) :
        text_widget . _get_tk_entity ( ) . delete ( widget . _get_tk_entity ( ) )
    def gui_remove_mark_from_text_widget ( self , text_widget , mark_name ) :
        text_widget . _get_tk_entity ( ) . mark_unset ( mark_name )
    def gui_delete_text_from_mark_to_end_of_text_widget ( self , text_widget , mark_name ) :
        text_widget . _get_tk_entity ( ) . delete ( mark_name , self . _tkinter . END )

    ##################################################################################################

    def gui_pack ( self , widget , padx , pady , fill , side ) :
        widget . _get_tk_entity ( ) . pack ( padx = padx , pady = pady , fill = fill , side = side )

    ##################################################################################################

    def gui_grid ( self , widget , column , row , sticky ) :
        widget . _get_tk_entity ( ) . grid ( column = column , row = row , sticky = sticky )

    ##################################################################################################

    def gui_key_up_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<Up>' , handler . _tk_event_handler )
        self . _mediator . gui_key_up_event_reply ( handler )
    def gui_key_down_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<Down>' , handler . _tk_event_handler )
        self . _mediator . gui_key_down_event_reply ( handler )
    def gui_key_return_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<Return>' , handler . _tk_event_handler )
        self . _mediator . gui_key_return_event_reply ( handler )
    def gui_key_escape_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<Escape>' , handler . _tk_event_handler )
        self . _mediator . gui_key_escape_event_reply ( handler )
    def gui_key_space_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<space>' , handler . _tk_event_handler )
        self . _mediator . gui_key_space_event_reply ( handler )
    def gui_key_backspace_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<BackSpace>' , handler . _tk_event_handler )
        self . _mediator . gui_key_backspace_event_reply ( handler )
    def gui_key_f5_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<F5>' , handler . _tk_event_handler )
        self . _mediator . gui_key_f5_event_reply ( handler )
    def gui_key_ctrl_f5_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<Control-F5>' , handler . _tk_event_handler )
        self . _mediator . gui_key_ctrl_f5_event_reply ( handler )
    def gui_mouse_button_1_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind ( '<Button-1>' , handler . _tk_event_handler )
        self . _mediator . gui_mouse_button_1_event_reply ( handler )
    def gui_global_mouse_button_1_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<Button-1>' , handler . _tk_event_handler )
        self . _mediator . gui_global_mouse_button_1_event_reply ( handler )
    def gui_any_key_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind_all ( '<Key>' , handler . _tk_event_handler )
        self . _mediator . gui_any_key_event_reply ( handler )
    def gui_focus_in_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind ( '<FocusIn>' , handler . _tk_event_handler )
        self . _mediator . gui_focus_in_event_reply ( handler )
    def gui_focus_out_event_request ( self , widget , handler_func ) :
        handler = event_handler ( handler_func , self . _scheduler )
        widget . _get_tk_entity ( ) . bind ( '<FocusOut>' , handler . _tk_event_handler )
        self . _mediator . gui_focus_out_event_reply ( handler )

    ##################################################################################################

    def gui_set_background_color ( self , widget , color ) :
        widget . _get_tk_entity ( ) . configure ( background = color )
    def gui_push_background_color ( self , widget ) :
        widget . _bg_color = widget . _get_tk_entity ( ) . cget ( 'background' )
    def gui_pop_background_color ( self , widget ) :
        widget . _get_tk_entity ( ) . configure ( background = widget . _bg_color )
    def gui_move_focus_to ( self , widget ) :
        widget . _get_tk_entity ( ) . focus ( )
    def gui_set_widget_font_weight_to_bold ( self , widget ) :
        font = self . _tkfont . Font ( font = widget . _get_tk_entity ( ) . cget ( 'font' ) )
        font . configure ( weight = 'bold' )
        widget . _get_tk_entity ( ) . configure ( font = font . name )
    def gui_push_widget_font ( self , widget ) :
        widget . _font = widget . _get_tk_entity ( ) . cget ( 'font' )
    def gui_pop_widget_font ( self , widget ) :
        widget . _get_tk_entity ( ) . configure ( font = widget . _font )
